package chapter08

/**
  * 模式匹配
  */
object Test01_PatternMatchBase {
  def main(args: Array[String]): Unit = {

    //1.基本定义用法
    val x: Int = 2
    val y: String = x match {
      case 1 => "one"
      case 2 => "two"
      case 3 => "three"
      case _ => "other"

    }
    println(y)

    //2.示例：用模式匹配实现简单的二元运算
    val a = 20
    val b = 5

    def matchDualOp(op: Char) = op match {
      case '+' => a + b
      case '-' => a - b
      case '*' => a * b
      case '/' => a / b
      case _ => -1
    }

    println(matchDualOp('+'))
    println(matchDualOp('-'))
    println(matchDualOp('*'))
    println(matchDualOp('/'))
    println(matchDualOp('a'))


    //2.模式守卫
    // 求一个整数的绝对值
    def abs(num: Int): Int = {
      num match {
        case i if i >= 0 => i
        case i if i < 0 => -i
      }
    }

    println(abs(-17))


  }


}
